package com.qyhl.webtv.module_live.teletext.nanbu;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.entity.live.NanBuLiveTagBean;
import com.qyhl.webtv.commonlib.utils.view.NoScrollViewPager;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.qyhl.webtv.module_live.teletext.nanbu.fragment.NanBuLiveFragment;
import com.weavey.loading.lib.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NanBuLiveListFragment extends BaseFragment implements NanBuLiveListContract.NanBuLiveListView {
    @BindView(R2.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    NoScrollViewPager viewPager;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.back_btn)
    ImageView backBtn;
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.title_layout)
    RelativeLayout titleLayout;

    private NanBuLiveListPresenter mPresenter;

    private String titleName;

    private boolean isActivity;

    public void setTitle(String title) {
        this.titleName = title;
    }

    public void setActivity(boolean activity) {
        isActivity = activity;
    }

    public static NanBuLiveListFragment newInstance() {
        NanBuLiveListFragment fragment = new NanBuLiveListFragment();
        fragment.setTitle("");
        fragment.setActivity(false);
        return fragment;
    }

    public static NanBuLiveListFragment newInstance(String title) {
        NanBuLiveListFragment fragment = new NanBuLiveListFragment();
        fragment.setTitle(title);
        fragment.setActivity(false);
        return fragment;
    }

    public static NanBuLiveListFragment newInstance(String title, boolean isActivity) {
        NanBuLiveListFragment fragment = new NanBuLiveListFragment();
        fragment.setTitle(title);
        fragment.setActivity(isActivity);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.live_fragment_nanbu_live, null);
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new NanBuLiveListPresenter(this);

        if (StringUtils.isNotEmpty(titleName)) {
            titleLayout.setVisibility(View.VISIBLE);
            title.setText(titleName);
        } else {
            titleLayout.setVisibility(View.GONE);
        }

        if (isActivity) {
            backBtn.setVisibility(View.VISIBLE);
        } else {
            backBtn.setVisibility(View.INVISIBLE);
        }

        mPresenter.getTagList();
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getTagList();
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setTagList(List<NanBuLiveTagBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        List<String> nameList = new ArrayList<>();
        List<Fragment> fragmentList = new ArrayList<>();

        nameList.add("全部");
        fragmentList.add(NanBuLiveFragment.newInstance(""));

        if (beanList != null && beanList.size() > 0) {
            for (int i = 0; i < beanList.size(); i++) {
                nameList.add(beanList.get(i).getTypeName());
                fragmentList.add(NanBuLiveFragment.newInstance(beanList.get(i).getId() + ""));
            }
        }

        viewPager.setAdapter(new Adapter(getChildFragmentManager(), fragmentList, nameList));
        tabLayout.setViewPager(viewPager);
    }

    @Override
    public void setTagError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(getContext())) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }

    @OnClick(R2.id.back_btn)
    public void onClick() {
        if (getActivity() instanceof NanbuLiveListActivity) {
            ((NanbuLiveListActivity) getActivity()).doFinish();
        }
    }

    static class Adapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        private List<String> names;

        public Adapter(FragmentManager fm, List<Fragment> fragmentList, List<String> names) {
            super(fm);
            this.fragmentList = fragmentList;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position);
        }
    }

    @Override
    public void initImmersionBar() {

    }
}
